//
//  RYChatViewController.m
//  SanYiTeaching
//
//  Created by ChenHao on 2019/7/31.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "RYChatBaseViewController.h"
#import "RYChatListViewController.h"
#import "RYChatViewController.h"
#import "ChatTViewController.h"
#import "RYChatBaseView.h"

@interface RYChatBaseViewController ()
{
    UILabel *lable;// 最近联系人几个字lable
    UIButton *btn;// 切换界面btn
    UIView *searchView ;
    ChatTViewController *vc;
    RYChatViewController *chatVC;
}
@end

@implementation RYChatBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //覆盖导航栏view
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 215, 65)];
//    view.backgroundColor = [UIColor colorWithRed:228/255 green:228/255 blue:228/255 alpha:1];
    view.backgroundColor = [UIColor colorWithHexString:@"#E4E4E4"];
    [self.view addSubview:view];
    
    //搜索框加最近联系人的整体view
    searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 55, 215, 75)];
    searchView.backgroundColor = [UIColor colorWithHexString:@"#E4E4E4"];

    [self.view addSubview:searchView];

    //搜索矿所在的view
    UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 155, 30)];
    inputView.backgroundColor = [UIColor whiteColor];
    [searchView addSubview:inputView];
    inputView.layer.masksToBounds = YES;
    inputView.layer.cornerRadius = 15;
    
    UIImageView *searchImgaeView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 6, 18, 18)];
    searchImgaeView.image = [UIImage imageNamed:@"chat_search"];
    [inputView addSubview:searchImgaeView];
    
    UITextField *searchField = [[UITextField alloc]initWithFrame:CGRectMake(45, 6, 100, 18)];
    searchField.placeholder = @"搜索";
    searchField.font = [UIFont systemFontOfSize:13];
    [inputView addSubview:searchField];
    
    btn = [[UIButton alloc]initWithFrame:CGRectMake(180, 4, 22, 22)];
//    btn.backgroundColor = [UIColor blueColor];
    [btn setImage:[UIImage imageNamed:@"chat_pushBtn"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pushToTongChatVCClick:) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:btn];
    
    lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 155, 15)];
    lable.text = @"最近联系人";
    lable.font = [UIFont systemFontOfSize:13];
    lable.textColor = [UIColor colorWithHexString:@"#333333"];
    [searchView addSubview:lable];
    
    RYChatListViewController *ListVC = [[RYChatListViewController alloc] init];
    [ListVC viewWillAppear:YES];
    ListVC.view.frame = CGRectMake(0, 130, 215, APP_HEIGHT - 20);
    ListVC.view.userInteractionEnabled = YES;
    ListVC.chooseChatListPeople = ^(RCUserInfo * _Nonnull userInfo) {
        [self setChatWithOnePeopleWithUserinfo:userInfo];
    };
    [self.view addSubview:ListVC.view];
    [self addChildViewController:ListVC];
    
//    RYChatViewController *chatVC = [[RYChatViewController alloc] init];
//    chatVC.conversationType = ConversationType_PRIVATE;
//    chatVC.targetId = @"sanyi-p-27";
//    chatVC.view.frame = CGRectMake(215, 0, APP_WIDTH-300, APP_HEIGHT - 20);
//    chatVC.view.userInteractionEnabled = YES;
//    [self.view addSubview:chatVC.view];
//    [self addChildViewController:chatVC];
    
    RYChatBaseView *vc = [[RYChatBaseView alloc]initWithFrame:CGRectMake(215, 20, APP_WIDTH-295, APP_HEIGHT - 20)];
    vc.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:vc];
}
-(void)setChatWithOnePeopleWithUserinfo:(RCUserInfo *)model{
    if (chatVC != nil) {
        [chatVC.view removeFromSuperview];
        chatVC = nil;
    }
    chatVC = [[RYChatViewController alloc] init];
    chatVC.conversationType = ConversationType_PRIVATE;
    chatVC.targetId = model.userId;
    chatVC.UserName = model.name;
    chatVC.view.frame = CGRectMake(215, 0, APP_WIDTH-300, APP_HEIGHT - 20);
    chatVC.view.userInteractionEnabled = YES;
    [self.view addSubview:chatVC.view];
    [self addChildViewController:chatVC];
}

-(void)pushToTongChatVCClick:(UIButton *)sender{
    vc = [[ChatTViewController alloc]init];
    vc.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    vc.popToChatListView = ^{
        [self deletTongXunLuVCWithAnimation];
    };
    [self.view addSubview:vc.view];
    [self addChildViewController:vc];
    [UIView animateWithDuration:1 animations:^{
        self->vc.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    }];

}
-(void)deletTongXunLuVCWithAnimation{
    
    [UIView animateWithDuration:1 animations:^{
        self->vc.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    } completion:^(BOOL finished) {
        [self->vc.view removeFromSuperview];
        self->vc = nil;
    }];
}
@end
